import Vue from 'vue'
Vue.filter('dateFormat', function (timestamp, mode = 0) {
  const date = new Date(timestamp)
  // 获取年月日
  const year = date.getFullYear()
  const month = (date.getMonth() + 1 + '').padStart(2, '0')
  const day = (date.getDate() + '').padStart(2, '0')
  // 时分秒
  const hour = (date.getHours() + '').padStart(2, '0')
  const minute = (date.getMinutes() + '').padStart(2, '0')
  const second = (date.getSeconds() + '').padStart(2, '0')
  // mode 为 0 是年月日, 为1是时分秒
  if (mode === 0) {
    return `${year}-${month}-${day}`
  } else if (mode === 1) {
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
  }
})
Vue.filter('timeFormat', function (time, format, mode = '') {
  // console.log(arguments)
  if (format === 'second') { time = time * 1000 }
  // console.log(time)
  const second = (Math.floor(time / 1000 % 60) + '').padStart(2, '0')
  const minute = (Math.floor(time / 1000 / 60 % 60) + '').padStart(2, '0')
  const hour = (Math.floor(time / 1000 / 60 / 60) + '').padStart(2, '0')
  // 结果
  const result = []
  result.push(minute, ':', second)
  if (mode === 'hour') result.unshift(hour, ':')
  return result.join('')
})
Vue.filter('tenThousand', function (time) {
  if (time < 10000) return time
  return Math.floor(time / 10000) + '万'
  // return time
})
// 把↵转换成<br>
Vue.prototype.$switchEnter = function (str) {
  // str.replace('↵', '222')
  str = str.replace(/\n/g, '<br/>')
  return str
}
